#include <allegro.h>
#include <stdio.h>

#define GFX_W 800
#define GFX_H 600
#define NB_IMAGES 4

struct Sandra {
	BITMAP * img_right[NB_IMAGES];
	BITMAP * img_left[NB_IMAGES];
	BITMAP * jump_right;
	BITMAP * jump_left;
	BITMAP * stand_right;
	BITMAP * stand_left;
	int x, y;
	int v;
	int d;

	int direction; // 0:right 1:left 2:up
};
typedef struct Sandra Sandra;


//utils
int get_pixel(BITMAP* Buffer,int x,int y);


//Module Background
BITMAP* load_background_bmp(char chemin_background[]);
BITMAP* load_background1_bmp(char chemin_background_col[]);
void draw_background(BITMAP *Background,BITMAP*Buffer);
void destroy_background(BITMAP * Background);
//Module Music
SAMPLE* load_music(char chemin_music[]);
SAMPLE* load_musicc(char chemin_musicc[]);
void play_music(SAMPLE *music);
void play_musicc(SAMPLE *wolf);
void destroy_music(SAMPLE *music);
//Module Mario
void load_sandra(Sandra *sandra);
void destroy_sandra(Sandra *sandra);
void init_sandra(Sandra *sandra);

void move_sandra_right(Sandra *sandra,int *compteur,BITMAP * Background,int pix1);
void move_sandra_left(Sandra *sandra,int *compteur,BITMAP * Background,int pix2);
void jump_sandra(Sandra *sandra,BITMAP * Background,int pix3); //move up
void stand_sandra(Sandra *sandra,BITMAP * Background );

void move_sandra(Sandra *sandra,int *compteur,BITMAP * Background,int pix1,int pix2,int pix3,int pix4);
void draw_sandra(Sandra sandra,BITMAP *Buffer, int compteur);




int main() {

	char chemin_background[]="../images/BACKGROUND.bmp";
	char chemin_background_col[]="../images/BACKGROUNDCol.bmp";

	BITMAP *Buffer;
	BITMAP *Background=NULL;
	BITMAP *BackgroundCol=NULL;
	Sandra sandra;

	int compteur;


	int pix1,pix2,pix3,pix4;



	/* General initialization function */
	allegro_init();
	if (install_keyboard() == -1) {
		allegro_message("Error! %s", allegro_error);
		return (-1);
	}

	//INSTALL MOUSE
	install_mouse() ;
	if(install_mouse() == -1) {

	allegro_message("Erreur ! %s", allegro_error) ;

	return-1 ;
	}
	set_color_depth(24);
	if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, GFX_W, GFX_H, 0, 0) != 0) {
		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); /* here’s the command for text mode */
		allegro_message("Unable to initialize video mode!\n%s\n", allegro_error);
		return (1);
	}

	Buffer = create_bitmap(8800, SCREEN_H);
	//loading images
	//loading mario
	load_sandra(&sandra);/*
	img0= load_bitmap("../images/Menu.bmp", NULL);
	img1= load_bitmap("../images/Menu1.bmp", NULL);
	img2= load_bitmap("../images/Menu2.bmp", NULL);*/

	//loading background + background collision
	 BackgroundCol=load_background1_bmp(chemin_background_col);
	Background=load_background_bmp(chemin_background);
	//loading background collision Map


	//init position & direction
	compteur = 0;
	init_sandra(&sandra);

	//Voulume SOUND

	//Load music


	//TODO add pixel_down
	/*pix1=get_pixel(BackgroundCol,sandra.x+ sandra.img_right[0]->w,sandra.y +sandra.img_right[0]->h /2);
		pix2=get_pixel(BackgroundCol,sandra.x,sandra.y + sandra.img_left[0]->h /2);
		pix3=get_pixel(BackgroundCol,sandra.x+ sandra.img_right[3]->w /2,sandra.y);
		pix4=get_pixel(BackgroundCol,sandra.x+ sandra.img_right[0]->w /2,sandra.y + sandra.img_right[0]->h);
	printf ("pixel right est :%d \n",pix1);
	printf ("pixel left est :%d \n",pix2);
	printf ("pixel up est :%d \n",pix3);
	printf ("pixel down est : %d \n",pix4);*/
	while (!key[KEY_ESC]) {

		//vider le buffer

		clear_bitmap(Buffer);

		//play music



 		//moving mario
		pix1=get_pixel(BackgroundCol,sandra.x+ (sandra.img_right[0]->w-7),sandra.y +sandra.img_right[0]->h /2);
		pix2=get_pixel(BackgroundCol,sandra.x,sandra.y + sandra.img_left[0]->h /2);
		pix3=get_pixel(BackgroundCol,sandra.x+ sandra.img_right[3]->w /2,sandra.y);
		pix4=get_pixel(BackgroundCol,sandra.x+ sandra.img_right[0]->w /2,(sandra.y-10) + (sandra.img_right[0]->h-50));

		move_sandra(&sandra,&compteur,BackgroundCol,pix1,pix2,pix3,pix4);

		//drawing background

		draw_background(BackgroundCol,Buffer);
		//draw_background(Background,Buffer);

		//drawing sandra

		draw_sandra(sandra,Buffer,compteur);
		//blit Buffer to screen

		blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);

		if (compteur == 10000)
			compteur = 0;
	}
	//destroy Sandra

	destroy_sandra(&sandra);
	destroy_background(Background);
	destroy_bitmap(Buffer);
	return (0);
}
END_OF_MAIN();
//Music MODULE




/*
 * module background
 */

BITMAP* load_background_bmp(char chemin_background[])
{
	BITMAP* Background;

	Background=load_bitmap(chemin_background, NULL);
	if(Background == NULL){
		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
		allegro_message("Unable to load image background!\n%s\n", allegro_error);
		return (NULL);
	}
	return (Background);

}
BITMAP* load_background1_bmp(char chemin_background_col[])
{
	BITMAP* BackgroundCol;

	BackgroundCol=load_bitmap(chemin_background_col, NULL);
	if(BackgroundCol == NULL){
		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
		allegro_message("Unable to load image background col!\n%s\n", allegro_error);
		return (NULL);
	}
	return (BackgroundCol);

}



void draw_background(BITMAP *Background,BITMAP*Buffer)
{
	blit(Background, Buffer, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
}

void destroy_background(BITMAP * Background)
{
	destroy_bitmap(Background);
}

/*
 * module sandra
 */

void load_sandra(Sandra *sandra)
{
	int i;
	char buf[50];
	for (i = 0; i < NB_IMAGES; i++) {
		sprintf(buf, "../images/right%d.bmp", i);
		sandra->img_right[i] = load_bitmap(buf, NULL);
		sprintf(buf, "../images/left%d.bmp", i);
		sandra->img_left[i] = load_bitmap(buf, NULL);
	}
	sandra->jump_right = load_bitmap("../images/jump_right.bmp",NULL);
	sandra->jump_left = load_bitmap("../images/jump_left.bmp",NULL);
	sandra->stand_right = load_bitmap("../images/stand_right.bmp",NULL);
	sandra->stand_left = load_bitmap("../images/stand_left.bmp",NULL);
}

void destroy_sandra(Sandra *sandra)
{
	int i;
	for (i = 0; i < NB_IMAGES; i++) {
		destroy_bitmap(sandra->img_right[i]);
		destroy_bitmap(sandra->img_left[i]);
	}

}
void init_sandra(Sandra *sandra)
{
	sandra->direction = 0;
	sandra->x = 20;
	sandra->y = 450;
}
void move_sandra_right(Sandra *sandra,int *compteur,BITMAP * Background,int pix1)
{
	 if ((mouse_b & 1) && pix1!=255) {


			*compteur+=1;
			(sandra->x)+=(0.9);
			sandra->direction = 0; //right




	}

	if (key[KEY_RIGHT]  && pix1!=255) {
		*compteur+=1;

		(sandra->x)+=1;
		sandra->direction = 0; //right

		sandra->d=1;
				//printf("\n pixel_right:%d",pix1);
	}


}

void move_sandra_left(Sandra *sandra,int *compteur,BITMAP * Background,int pix2)
{
	if ((mouse_b & 2)  && pix2 != 255)
	{
		*compteur+=1;

		(sandra->x)-=1;
		sandra->direction = 1; //left


	}

	if (key[KEY_LEFT] && pix2 != 255)
	{
		*compteur+=1;

		(sandra->x)-=1;
		sandra->direction = 1; //left
		sandra->d=1;

	}

}
void jump_sandra(Sandra *sandra,BITMAP * Background,int pix3)
{

	if (key[KEY_UP] && (sandra->y > 0) && pix3!=255) {

		(sandra->y)-=5;


		sandra->direction = 2; //jump
		sandra->v=(-16);

		if (key[KEY_RIGHT] && key[KEY_UP])
		{

		(sandra->y)-=5;


		sandra->direction = 2; //jump
		sandra->v=(-11);
		(sandra->x)+=1;


		}
		//printf ("up :%d \n",pix3);

	}


}

void stand_sandra(Sandra *sandra,BITMAP * Background )
{
	if (!key[KEY_RIGHT] && !key[KEY_LEFT]) {
	sandra->d=0;
	}
}
void move_sandra(Sandra *sandra,int *compteur,BITMAP * Background,int pix1,int pix2,int pix3,int pix4)
{


	move_sandra_right(sandra,compteur,Background,pix1);
	move_sandra_left(sandra,compteur,Background,pix2);
	jump_sandra(sandra,Background,pix3);
	stand_sandra(sandra,Background );

	//add gravity*/
	if (pix4!=255)
	{
	(sandra->v)+=18;
	(sandra->y)+=(sandra->v);

	while ((sandra->y)>750)
	{
	(sandra->y)-=1;}}

}
void draw_sandra(Sandra sandra,BITMAP *Buffer, int compteur)
{
	static int last_direction;
	 if (sandra.d== 0 && sandra.direction == 0 ){

		draw_sprite(Buffer, sandra.stand_right,sandra.x, sandra.y);
		last_direction=0;
	}
	if (sandra.d== 0 && sandra.direction == 1){

		draw_sprite(Buffer, sandra.stand_left,sandra.x, sandra.y);
		last_direction=0;
	}

	if (sandra.d== 1 && sandra.direction == 0){ //moving right

		draw_sprite(Buffer, sandra.img_right[compteur % (NB_IMAGES-1) ],sandra.x, sandra.y);
		last_direction=0;
	}

	if(sandra.d== 1 && sandra.direction == 1){ //moving left

		draw_sprite(Buffer, sandra.img_left[compteur % (NB_IMAGES-1) ],sandra.x, sandra.y);
		last_direction=1;
	}
	if (sandra.direction == 2 && last_direction==0) {	//jumping right

		draw_sprite(Buffer, sandra.jump_right,sandra.x, sandra.y);
	}
	if (sandra.direction == 2 && last_direction==1) {	//jumping left

		draw_sprite(Buffer, sandra.jump_left,sandra.x, sandra.y);
	}
}

/*
 * module collision
 */




int get_pixel(BITMAP* Buffer,int x,int y)
{
	int tmp;
	tmp=getpixel(Buffer,x,y);
	return((getr(tmp) + getb(tmp) + getg(tmp)) / 3);
}




